package com.swift.application.controller;

import com.swift.core.constant.CommonEnum;
import com.swift.core.response.ResponseResult;
import com.swift.core.user.request.UserReq;
import com.swift.core.user.response.UserRes;
import com.swift.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Ldanniel
 * @description :
 * @date 2022/07/09 21:40
 * @since open jdk 1.8
 */
@RestController
@RequestMapping(value = "/users")
public class UserController {
    private final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @PostMapping("/registry")
    public ResponseResult<UserRes> userRegistry(@RequestBody UserReq req) {
        logger.info("执行用户注册");
        ResponseResult<UserRes> response = new ResponseResult<>(CommonEnum.SUCCESS);
        try {
            userService.userAdd(req);
        } catch (Exception e) {
            logger.error("用户注册异常",e);
            response.setMessage(CommonEnum.FAILURE.getDescription());
            response.setStatus(CommonEnum.FAILURE.getCode());
        }
        return response;
    }

    @PostMapping("/getUserInfo")
    public ResponseResult<UserRes> getUserInfo(@RequestBody UserReq req) {
        logger.info("执行用户注册");
        ResponseResult<UserRes> response = new ResponseResult<>(CommonEnum.SUCCESS);
        try {
            UserRes userRes = userService.queryUserInfoById(Integer.parseInt(req.getId()));
            response.setData(userRes);
        } catch (Exception e) {
            response.setMessage(CommonEnum.FAILURE.getDescription());
            response.setStatus(CommonEnum.FAILURE.getCode());
        }
        return response;
    }
//    /**
//     * 单点登录测试
//     *
//     * @param authentication
//     * @return
//     */
//    @GetMapping("/getCurrentUser")
//    public Object getCurrentUser(Authentication authentication) {
//        return authentication;
//    }
}
